<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="资产名称" prop="assetName">
        <el-input
          v-model="queryParams.assetName"
          placeholder="请输入资产名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产编码" prop="assetCode">
        <el-input
          v-model="queryParams.assetCode"
          placeholder="请输入资产编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一编码" prop="gs1Code">
        <el-input
          v-model="queryParams.gs1Code"
          placeholder="请输入统一编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用人" prop="userPersonnel">
        <el-select
          v-model="queryParams.userPersonnel"
          placeholder="请选择使用人"
          filterable
          clearable
        >
          <el-option
            v-for="users in personnels"
            :key="users.value"
            :label="users.label"
            :value="users.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用部门" prop="userDepartment">
        <el-select
          v-model="queryParams.userDepartment"
          placeholder="请选择使用部门"
          filterable
          clearable
        >
          <el-option
            v-for="users in departments"
            :key="users.value"
            :label="users.label"
            :value="users.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数量" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brandName">
        <el-input
          v-model="queryParams.brandName"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原值" prop="originalValue">
        <el-input
          v-model="queryParams.originalValue"
          placeholder="请输入原值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计量单位" prop="unit">
        <el-select
          v-model="queryParams.unit"
          placeholder="请选择计量单位"
          clearable
        >
          <el-option
            v-for="dict in dict.type.asset_unit"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="会计号" prop="accountingCode">
        <el-input
          v-model="queryParams.accountingCode"
          placeholder="请输入会计凭证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="财务号" prop="finance">
        <el-input
          v-model="queryParams.finance"
          placeholder="请输入财务卡片号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存放地点" prop="storagePlace">
        <el-input
          v-model="queryParams.storagePlace"
          placeholder="请输入存放地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="取得方式" prop="acquisitionMethod">
        <el-select
          v-model="queryParams.acquisitionMethod"
          placeholder="请选择取得方式"
          clearable
        >
          <el-option
            v-for="dict in dict.type.asset_acquisition_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="取得日期">
        <el-date-picker
          v-model="daterangeAcquisitionDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="使用方向" prop="useDirection">
        <el-select
          v-model="queryParams.useDirection"
          placeholder="请选择使用方向"
          clearable
        >
          <el-option
            v-for="dict in dict.type.asset_use_direction"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用状况" prop="useStatus">
        <el-select
          v-model="queryParams.useStatus"
          placeholder="请选择使用状况"
          clearable
        >
          <el-option
            v-for="dict in dict.type.asset_use_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="启用日期">
        <el-date-picker
          v-model="daterangeStartDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="资产分类" prop="assetClassification">
        <treeselect
          v-model="queryParams.assetClassification"
          :options="assetClassificationOptions"
          :show-count="true"
          :flat="true"
          placeholder="请选择资产分类（点三角形或者直接搜索）"
          style="width: 215px"
        />
      </el-form-item>
      <el-form-item label="归属部门" prop="useDept">
        <treeselect
          v-model="queryParams.useDept"
          :options="deptOptions"
          :show-count="true"
          placeholder="请选择管理部门(归属部门)"
          style="width: 215px"
        />
      </el-form-item>
      <el-form-item label="归属账号" prop="useName">
        <el-select
          v-model="queryParams.useName"
          placeholder="请选择归属账号"
          filterable
          clearable
        >
          <el-option
            v-for="users in listUsers"
            :key="users.value"
            :label="users.label"
            :value="users.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="rfid编码" prop="rfidCode">
        <el-input
          v-model="queryParams.rfidCode"
          placeholder="rfid编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管理部门" prop="manageDept">
        <el-input
          v-model="queryParams.manageDept"
          placeholder="管理部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原资产编码" prop="oldAssetCode">
        <el-input
          v-model="queryParams.oldAssetCode"
          placeholder="请输入原资产编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!--      <el-form-item label="scanCodeStr" prop="scanCodeStr">
        <el-input
          v-model="queryParams.scanCodeStr"
          placeholder="请输入原资产编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="rfidCodeStr" prop="rfidCodeStr">
        <el-input
          v-model="queryParams.rfidCodeStr"
          placeholder="请输入原资产编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manage:asset:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:asset:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:asset:remove']"
          >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manage:asset:export']"
          >导出
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['manage:asset:add']"
        >批量操作</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="batchPrint"
          :disabled="multiple"
          v-hasPermi="['manage:asset:add']"
          >批量打印</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="assetList"
      @selection-change="handleSelectionChange"
      @sort-change="handleSortChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="rfid编码"
        align="center"
        class-name="small-padding fixed-width"
        width="100"
        :sort-orders="['descending', 'ascending']"
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-grid"
            @click="getCodeUrl(scope.row)"
            v-hasPermi="['manage:asset:edit']"
          >
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="资产名称"
        align="center"
        prop="assetName"
        :sort-orders="['descending', 'ascending']"
        sortable="custom"
      />
      <el-table-column
        label="资产编码"
        align="center"
        prop="assetCode"
        :sort-orders="['descending', 'ascending']"
        sortable="custom"
        width="220"
      />
      <!--      <el-table-column label="统一编码" align="center" prop="gs1Code" :sort-orders="['descending','ascending']" sortable="custom"/>-->
      <el-table-column
        label="资产分类"
        align="center"
        prop="assetClassificationName"
      />
      <el-table-column
        label="数量"
        align="center"
        prop="number"
        :sort-orders="['descending', 'ascending']"
        sortable="custom"
      />
      <el-table-column
        label="原值"
        align="center"
        prop="originalValue"
        :sort-orders="['descending', 'ascending']"
        sortable="custom"
      />
      <!--      <el-table-column label="品牌" align="center" prop="brandName" />
            <el-table-column label="规格型号" align="center" prop="modelName" />-->
      <!--         <el-table-column label="资产分类" align="center" prop="assetClassification" />-->
      <!--      <el-table-column label="计量单位" align="center" prop="unit" :sort-orders="['descending','ascending']" sortable="custom">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_unit" :value="scope.row.unit"/>
        </template>
      </el-table-column>
      <el-table-column label="取得方式" align="center" prop="acquisitionMethod" :sort-orders="['descending','ascending']" sortable="custom">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_acquisition_method" :value="scope.row.acquisitionMethod"/>
        </template>
      </el-table-column>-->
      <el-table-column
        label="取得日期"
        align="center"
        prop="acquisitionDate"
        width="180"
        :sort-orders="['descending', 'ascending']"
        sortable="custom"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acquisitionDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="使用方向" align="center" prop="useDirection" :sort-orders="['descending','ascending']" sortable="custom">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_use_direction" :value="scope.row.useDirection"/>
        </template>
      </el-table-column>
      <el-table-column label="使用状况" align="center" prop="useStatus" :sort-orders="['descending','ascending']" sortable="custom">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_use_status" :value="scope.row.useStatus"/>
        </template>
      </el-table-column>-->
      <!--      <el-table-column label="启用日期" align="center" prop="startDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>-->
      <el-table-column
        label="使用部门"
        align="center"
        prop="userDepartmentName"
        :sort-orders="['descending', 'ascending']"
        sortable="custom"
      />
      <el-table-column
        label="使用人"
        align="center"
        prop="userPersonnelName"
        :sort-orders="['descending', 'ascending']"
        sortable="custom"
      />
      <el-table-column
        label="存放地点"
        align="center"
        prop="storagePlace"
        :sort-orders="['descending', 'ascending']"
        sortable="custom"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="230"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:asset:edit']"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:asset:remove']"
            >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-printer"
            @click="handlePrint(scope.row)"
            v-hasPermi="['manage:asset:edit']"
            >打印
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-sort"
            v-hasPermi="['manage:asset:edit']"
            @click="handleUserList(scope.row)"
            >资产轨迹
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="950px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-collapse v-model="activeNames" accordion>
          <el-collapse-item title="基本信息" name="1">
            <el-col :span="12">
              <el-form-item label="资产名称" prop="assetName">
                <el-input
                  v-model="form.assetName"
                  placeholder="请输入资产名称"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="资产分类" prop="assetClassification">
                <treeselect
                  v-model="form.assetClassification"
                  :options="assetClassificationOptions"
                  :show-count="true"
                  :flat="true"
                  placeholder="请选择资产分类（点三角形或者直接搜索）"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="使用人" prop="userPersonnel">
                <el-select
                  v-model="form.userPersonnel"
                  placeholder="请选择使用人"
                  filterable
                  clearable
                >
                  <el-option
                    v-for="users in personnels"
                    :key="users.value"
                    :label="users.label"
                    :value="users.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="使用部门" prop="userDepartment">
                <el-select
                  v-model="form.userDepartment"
                  placeholder="请选择使用部门"
                  filterable
                  clearable
                >
                  <el-option
                    v-for="users in departments"
                    :key="users.value"
                    :label="users.label"
                    :value="users.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="数量" prop="number">
                <el-input-number
                  v-model="form.number"
                  placeholder="请输入数量"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="原值" prop="originalValue">
                <el-input-number
                  :precision="2"
                  v-model="form.originalValue"
                  placeholder="请输入原值"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="取得方式" prop="acquisitionMethod">
                <el-select
                  v-model="form.acquisitionMethod"
                  placeholder="请选择取得方式"
                >
                  <el-option
                    v-for="dict in dict.type.asset_acquisition_method"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="取得日期" prop="acquisitionDate">
                <el-date-picker
                  clearable
                  v-model="form.acquisitionDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择取得日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="计量单位" prop="unit">
                <el-select v-model="form.unit" placeholder="请选择计量单位">
                  <el-option
                    v-for="dict in dict.type.asset_unit"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-collapse-item>

          <el-collapse-item title="详细信息" name="2">
            <el-col :span="12">
              <el-form-item label="资产编码" prop="assetCode">
                <el-input
                  v-model="form.assetCode"
                  placeholder="请输入资产编码"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="统一编码" prop="gs1Code">
                <el-input v-model="form.gs1Code" placeholder="请输入统一编码" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="存放地点" prop="storagePlace">
                <el-input
                  v-model="form.storagePlace"
                  placeholder="请输入存放地点"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="品牌" prop="brandName">
                <el-input v-model="form.brandName" placeholder="请输入品牌" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规格型号" prop="modelName">
                <el-input
                  v-model="form.modelName"
                  placeholder="请输入规格型号"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="会计号" prop="accountingCode">
                <el-input
                  v-model="form.accountingCode"
                  placeholder="请输入会计凭证号"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="财务号" prop="finance">
                <el-input
                  v-model="form.finance"
                  placeholder="请输入财务卡片号"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="使用方向" prop="useDirection">
                <el-select
                  v-model="form.useDirection"
                  placeholder="请输入使用方向"
                >
                  <el-option
                    v-for="dict in dict.type.asset_use_direction"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="使用状况" prop="useStatus">
                <el-select
                  v-model="form.useStatus"
                  placeholder="请选择使用状况"
                >
                  <el-option
                    v-for="dict in dict.type.asset_use_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="启用日期" prop="startDate">
                <el-date-picker
                  clearable
                  v-model="form.startDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择开始使用日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="管理部门" prop="manageDept">
                <el-input
                  v-model="form.manageDept"
                  placeholder="请输入管理部门"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="notes">
                <el-input v-model="form.notes" placeholder="请输入备注" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="归属部门" prop="useDept">
                <treeselect
                  v-model="form.useDept"
                  :options="deptOptions"
                  :show-count="true"
                  placeholder="请选择管理部门(归属部门)"
                  style="height: 30px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="归属账号" prop="useName">
                <el-select
                  v-model="form.useName"
                  placeholder="请选择归属账号"
                  filterable
                  clearable
                >
                  <el-option
                    v-for="users in listUsers"
                    :key="users.value"
                    :label="users.label"
                    :value="users.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="原资产编码" prop="oldAssetCode">
                <el-input
                  v-model="form.oldAssetCode"
                  placeholder="请输入原资产编码"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item
                label="资产图片"
                prop="assetUrl"
                style="height: 200px"
              >
                <imageUpload :limit="1" v-model="form.assetUrl" />
              </el-form-item>
            </el-col>
          </el-collapse-item>
        </el-collapse>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="getCodeUrlOpen" width="400px">
      <table style="border-collapse: collapse" align="center">
        <tr class="coss" style="height: 80px; text-align: center">
          <td class="coss" colspan="2" style="text-align: center">
            <div align="center" id="qrCodeUrl"></div>
          </td>
        </tr>
      </table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改资产信息对话框 -->
    <!--    <el-dialog  :title="title" :visible.sync="openPrint" width="950px" append-to-body>
      <iframe
        id="proxy"
        ref="iframe"
        :src="htmlSrc"
        frameborder="0"
      />
    </el-dialog>-->
    <printDSVue
      :visible.sync="openPrint"
      :title="title"
      :assets="assets"
      append-to-body
    />

    <!-- 资产导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"  
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否批改
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-if="upload.isSubmitFileForm"
          type="primary"
          @click="submitFileForm"
          >确 定</el-button
        >
        <el-button
          v-if="!upload.isSubmitFileForm"
          type="primary"
          icon="el-icon-loading"
        ></el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入结果弹出框 -->
    <UploadResult :visible.sync="resultVisible" :data="uploadResult" />
  </div>
</template>

<script>
import {
  listAsset,
  getAsset,
  delAsset,
  addAsset,
  updateAsset,
  listassetClassification,
} from "@/api/manage/asset";
import { getPersonnelList } from "@/api/manage/personnel";
import { getDepartmentList } from "@/api/manage/department";
import { treeSelect } from "@/api/system/dept";
import { getUserlist } from "@/api/system/user";
import { getTemplateList } from "@/api/manage/fields";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import QRCode from "qrcodejs2";
import printDSVue from "./printDS3.vue";
import { getToken } from "@/utils/auth";
import UploadResult from "./uploadResult.vue";

export default {
  name: "Asset",
  dicts: [
    "asset_unit",
    "asset_acquisition_method",
    "asset_use_status",
    "asset_use_direction",
  ],
  components: { Treeselect, printDSVue, UploadResult },
  data() {
    return {
      // 资产导入参数
      upload: {
        // 是否显示弹出层（资产导入）
        open: false,
        // 弹出层标题（资产导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否禁用提交
        isSubmitFileForm: true,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/manage/asset/importData",
      },
      htmlSrc: "/ds.html",
      activeNames: "1",
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 资产信息表格数据
      assetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 资产分类树桩选项
      assetClassificationOptions: [],
      // 取得日期时间范围
      daterangeAcquisitionDate: [],
      // 启用日期时间范围
      daterangeStartDate: [],
      // 部门树桩选项
      deptOptions: [],
      // 归属账号下拉选项
      listUsers: [],
      // 是否显示二维码弹出层
      getCodeUrlOpen: false,
      // 打印弹出层
      openPrint: false,
      assets: [],
      // 打印模板选择项
      templateList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assetName: null,
        assetCode: null,
        gs1Code: null,
        number: null,
        brandName: null,
        modelName: null,
        assetClassification: null,
        originalValue: null,
        unit: null,
        accountingCode: null,
        finance: null,
        storagePlace: null,
        acquisitionMethod: null,
        acquisitionDate: null,
        useDirection: null,
        useStatus: null,
        startDate: null,
        useDept: null,
        assetUrl: null,
        useName: null,
        notes: null,
        gmtCreate: null,
        gmtModify: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        assetName: [
          { required: true, message: "资产名称不能为空", trigger: "blur" },
        ],
      },
      // 导入结果弹出框
      resultVisible: false,
      uploadResult: [],
      // 人员下拉
      personnels: [],
      // 部门下拉
      departments: [],
    };
  },
  created() {
    // this.getList();
    // this.getListAssetAll();
    // this.getTreeselect();
    // this.getUser();
    // this.getTemplateList();
    // this.getDepartment();
    // this.getPersonnel();
  },
  methods: {
    /**资产轨迹*/
    handleUserList: function (row) {
      this.$router.push("/assets/assetChange?assetId=" + row.id);
    },
    /*人员下拉选项*/
    getDepartment() {
      getDepartmentList().then((response) => {
        this.departments = response.rows;
      });
    },
    /*人员下拉选项*/
    getPersonnel() {
      getPersonnelList().then((response) => {
        this.personnels = response.rows;
      });
    },
    // 打印模板选择项
    getTemplateList() {
      getTemplateList().then((response) => {
        this.templateList = response.rows;
      });
    },
    handleSortChange(column) {
      this.queryParams.orderByColumn = column.prop; //查询字段是表格中字段名字
      if (this.queryParams.orderByColumn == "userDepartmentName") {
        this.queryParams.orderByColumn = "userDepartment";
      }
      if (this.queryParams.orderByColumn == "userPersonnelName") {
        this.queryParams.orderByColumn = "userPersonnel";
      }
      this.queryParams.isAsc = column.order; //动态取值排序顺序
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.upload.isSubmitFileForm = false;
      this.$refs.upload.submit();
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "manage/asset/importTemplate",
        {},
        `资产导入模板${new Date().getTime()}.xlsx`,
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.uploadResult = response.data.assetImportVoList;
      this.resultVisible = true;
      this.upload.open = false;
      this.upload.isUploading = false;
      this.upload.isSubmitFileForm = true;
      this.$refs.upload.clearFiles();
      // this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.reset();
      this.upload.title = "资产导入/批改";
      this.upload.open = true;
    },
    /** 修改按钮操作 */
    handlePrint(row) {
      this.reset();
      const id = row.id || this.ids;
      getAsset(id).then((response) => {
        this.form = response.data;
        this.assets = [row];
        this.openPrint = true;
        this.title = "资产卡片打印";
        // this.$nextTick(()=>{
        //   this.iframeWin=this.$refs.iframe.contentWindow;
        //   this.sendMessage();
        // })
      });
    },
    sendMessage() {
      setTimeout(() => {
        this.iframeWin.postMessage(
          {
            assets: this.assets,
          },
          "*",
        );
      }, 500);
    },
    /** 二维码按钮操作 */
    getCodeUrl(row) {
      this.reset();
      this.getCodeUrlOpen = true;
      this.title = "rfid编码（" + row.rfidCode + ")";
      // 生成二维码
      this.$nextTick(function () {
        console.log(document.getElementById("qrCodeUrl"));
        document.getElementById("qrCodeUrl").innerHTML = "";
        let qrCodeUrl = new QRCode("qrCodeUrl", {
          width: 200,
          height: 200,
          text: row.rfidCode,
          colorDark: "#000000",
          colorLight: "#fff",
        });
      });
    },
    /*归属账号下拉选项*/
    getUser() {
      getUserlist().then((response) => {
        this.listUsers = response.rows;
      });
    },
    /*部门树桩选项*/
    getTreeselect() {
      treeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /*资产分类树桩选项*/
    getListAssetAll() {
      listassetClassification().then((response) => {
        this.assetClassificationOptions = response.data;
      });
    },
    /** 查询资产信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (
        null != this.daterangeAcquisitionDate &&
        "" != this.daterangeAcquisitionDate
      ) {
        this.queryParams.params["beginAcquisitionDate"] =
          this.daterangeAcquisitionDate[0];
        this.queryParams.params["endAcquisitionDate"] =
          this.daterangeAcquisitionDate[1];
      }
      if (null != this.daterangeStartDate && "" != this.daterangeStartDate) {
        this.queryParams.params["beginStartDate"] = this.daterangeStartDate[0];
        this.queryParams.params["endStartDate"] = this.daterangeStartDate[1];
      }
      listAsset(this.queryParams).then((response) => {
        this.assetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.getCodeUrlOpen = false;
      this.openPrint = false;
      this.upload.isSubmitFileForm = true;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        assetName: null,
        assetCode: null,
        gs1Code: null,
        number: null,
        brandName: null,
        modelName: null,
        assetClassification: null,
        originalValue: null,
        unit: null,
        accountingCode: null,
        finance: null,
        storagePlace: null,
        acquisitionMethod: null,
        acquisitionDate: null,
        useDirection: null,
        useStatus: null,
        startDate: null,
        useDept: null,
        assetUrl: null,
        useName: null,
        notes: null,
        gmtCreate: null,
        gmtModify: null,
        isDelete: null,
      };
      this.resetForm("form");
      this.openPrint = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.reset();
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset();
      this.daterangeAcquisitionDate = [];
      this.daterangeStartDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.openPrint = false;
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.assets = selection;
    },
    /** 批量打印操作 */
    batchPrint() {
      this.openPrint = true;
      this.title = "资产卡片打印";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "资产卡片登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAsset(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "资产卡片修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAsset(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAsset(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除资产信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAsset(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "manage/asset/export",
        {
          ...this.queryParams,
        },
        `asset_${new Date().getTime()}.xlsx`,
      );
    },
  },
};
</script>

<style>
.el-collapse .el-collapse-item__header {
  font-size: 16px;
  font-weight: bold;
}
</style>
